﻿Public Class VSSForm

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "VSS ini|*.ini"
        OpenFileDialog1.FileName = VSS路径TextBox.Text
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            VSS路径TextBox.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.Filter = "EXE|*.exe"
        OpenFileDialog1.FileName = 快捷方式名TextBox.Text
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            快捷方式名TextBox.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub ShowMessage(ByVal strContent As String)
        MessageBox.Show(strContent, "VSS切换")
    End Sub

    Private Function CheckAll() As Boolean
        If 用户名TextBox.Text.Trim = "" Then
            ShowMessage("不能为空")
            用户名TextBox.Focus()
            Return False
        End If

        If 密码TextBox.Text.Trim = "" Then
            ShowMessage("不能为空")
            密码TextBox.Focus()
            Return False
        End If

        If VSS路径TextBox.Text.Trim = "" Then
            ShowMessage("不能为空")
            VSS路径TextBox.Focus()
            Return False
        End If

        'If System.IO.File.Exists(VSS路径TextBox.Text.Trim) = False Then
        '    ShowMessage("路径不存在")
        '    VSS路径TextBox.Focus()
        '    Return False
        'End If

        If 快捷方式名TextBox.Text.Trim = "" Then
            ShowMessage("不能为空")
            快捷方式名TextBox.Focus()
            Return False
        End If

        'If System.IO.File.Exists(快捷方式名TextBox.Text.Trim) = False Then
        '    ShowMessage("路径不存在")
        '    快捷方式名TextBox.Focus()
        '    Return False
        'End If

        If 最后一次目录TextBox.Text.Trim = "" Then
            ShowMessage("不能为空")
            最后一次目录TextBox.Focus()
            Return False
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
        Return True

    End Function

    Private Sub btn确定_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn确定.Click
        If CheckAll() = False Then
            Exit Sub
        End If
    End Sub

    Public Function GetVss() As VSS
        Dim vs As New VSS
        vs.USERNAME = 用户名TextBox.Text.Trim
        vs.PASSWORD = 密码TextBox.Text.Trim
        vs.VSSDIR = VSS路径TextBox.Text.Trim
        vs.VSSSETUPPATH = 快捷方式名TextBox.Text.Trim
        vs.LastDir = 最后一次目录TextBox.Text.Trim
        vs.EncodePage = Me.dg编码.SelectedRows(0).Cells(0).Value
        Return vs
    End Function

    Private _vs As VSS
    Public Sub SetVss(ByVal vs As VSS)
        _vs = vs
        用户名TextBox.Text = vs.USERNAME
        密码TextBox.Text = vs.PASSWORD
        快捷方式名TextBox.Text = vs.VSSSETUPPATH
        VSS路径TextBox.Text = vs.VSSDIR
        最后一次目录TextBox.Text = vs.LastDir
        
    End Sub

    Private Sub VSSForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cos() As System.Text.EncodingInfo = System.Text.Encoding.GetEncodings
        Dim count = cos.Length
        Dim j = 0
        While j < count
            Me.dg编码.Rows.Add(New Object() {cos(j).CodePage, cos(j).Name})
            j += 1
        End While

        Dim defaultEncoding As Integer = System.Text.Encoding.Default.WindowsCodePage
        If _vs IsNot Nothing Then
            defaultEncoding = _vs.EncodePage
        End If
        For Each dr As DataGridViewRow In Me.dg编码.Rows
            If dr.Cells(0).Value = defaultEncoding Then
                dr.Selected = True
                Me.dg编码.FirstDisplayedScrollingRowIndex = dr.Index
            End If
        Next

    End Sub

    Private Sub btn取消_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn取消.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btn快捷方式_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Escape Then
            Me.Close()
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
End Class